package com.example.web.controller.tag;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {
	private static final long serialVersionUID = 3947377370677692140L;
	
	public int doStartTag() throws JspException {
		
		StringBuilder pageInfo = new StringBuilder();
		try {
			
			PageModel model=new PageModel();
			model.setCpage(cpage);
			model.setPageSize(pageSize);
			model.setTotalCount(totalCount);
			
			
			JspWriter out = pageContext.getOut();			
			
			pageInfo.append("<script type=\"text/javascript\">  ");
            pageInfo.append(" var totalPage = 0;    ");
            pageInfo.append(" function gotopage(pageIndex, LastPage) {    ");
            pageInfo.append("       totalPage = LastPage;    ");
            pageInfo.append("       document.getElementById('CurrentPageIndex').value = pageIndex;         ");
            pageInfo.append("       document.getElementById('mvcpager_btnchk').click();    ");
            pageInfo.append(" }     ");
            pageInfo.append(" function checkInput(totalPage, isAjax) {     ");
            pageInfo.append("       totalPage = totalPage; ");
            pageInfo.append("       var pageIndexStr = document.getElementById('CurrentPageIndex').value;    ");
            pageInfo.append("       if (pageIndexStr == \"\")  {     ");
            pageInfo.append("           alert('请输入页码');    ");
            pageInfo.append("           document.getElementById('CurrentPageIndex').focus();    ");
            pageInfo.append("           document.getElementById('CurrentPageIndex').select();    ");
            pageInfo.append("           return false;    ");
            pageInfo.append("       }     ");
            pageInfo.append("       var r = new RegExp(\"^\\\\s*(\\\\d+)\\\\s*$\");");
            pageInfo.append("       var pageIndex = parseInt(pageIndexStr);");
            pageInfo.append("       if (r.test(pageIndex)) { ");
            pageInfo.append("           if (RegExp.$1 < 1 || RegExp.$1 > totalPage) { ");
            pageInfo.append("               alert(\"页索引超出范围！\");  ");
            pageInfo.append("               document.getElementById('CurrentPageIndex').focus(); ");
            pageInfo.append("               document.getElementById('CurrentPageIndex').select();  ");
            pageInfo.append("               return false; ");
            pageInfo.append("           } ");
            pageInfo.append("           if (isAjax) { ");
            pageInfo.append("               PostAjaxPage(pageIndex); ");
            pageInfo.append("               return false; ");
            pageInfo.append("           } else { ");
            pageInfo.append("               return true; ");
            pageInfo.append("           } ");
            pageInfo.append("        } ");
            pageInfo.append("       alert(\"页索引不是有效的数值！\");  ");
            pageInfo.append("       document.getElementById('CurrentPageIndex').focus(); ");
            pageInfo.append("       document.getElementById('CurrentPageIndex').select(); ");
            pageInfo.append("       return false; ");
            pageInfo.append("   } ");
            pageInfo.append("   function PostAjaxPage(pageIndex){ ");
            pageInfo.append("       var data='&CurrentPageIndex=' + pageIndex ;                ");
            pageInfo.append("       data=data + '&CurrentPageCss=' + document.getElementById('page.CurrentPageCss').value;     ");
            pageInfo.append("       data=data + '&DispPageCount=' + document.getElementById('page.DispPageCount').value;     ");
            pageInfo.append("       data=data + '&PageSize=' + document.getElementById('page.PageSize').value;     ");
            pageInfo.append("       getAjaxPageData(data)        ");
            pageInfo.append("   } ");
            pageInfo.append("  </script>");


	            if (model.getTotalPage() > 0 && model.getCpage() <=model.getTotalPage())
	            {
	                pageInfo.append("<div id=\"mvcpager\">");               
	                if (cpage > 1)
	                {
	                	// MessageFormat.format("我是{0},我来自{1},今年{2}岁", "中国人", "北京", "22");
	                	pageInfo.append(MessageFormat.format("<a  href=\"javascript:gotopage(1,{0})\" style=\"margin-right:10px;\">首页</a>",  model.getTotalPage()));
	                    pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;\">前页</a>", (model.getCpage() - 1), model.getTotalPage()));
	                }
	                else
	                {
	                	pageInfo.append("<a disabled=\"disabled\" style=\"margin-right:10px;\">首页</a>");
	                	pageInfo.append("<a disabled=\"disabled\" style=\"margin-right:10px;\">前页</a>");
	                }
	                if (model.getPageFirst() > 1)
	                {
	                    pageInfo.append(MessageFormat.format("<a   href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;text-decoration: none;\">.&nbsp;.</a>", (model.getPageFirst() - 1), model.getTotalPage()));
	                }

	                for(int i= 0; i < model.getDispPageCount(); i++)
	                {
	                    if ((model.getPageFirst() + i) <= model.getTotalPage())
	                    {
	                        if (model.getCpage() == (model.getPageFirst() + i))
	                        {
	                           
	                                pageInfo.append(MessageFormat.format("<a    href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;color:#FF0000;\">{0}</a>  ", (model.getPageFirst() + i), model.getTotalPage()));
	                           
	                        }
	                        else
	                        {
	                            pageInfo.append(MessageFormat.format("<a    href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;\">{0}</a>  ", (model.getPageFirst() + i), model.getTotalPage()));
	                        }
	                    }
	                }
	                if ((model.getPageFirst() + model.getDispPageCount()) <= model.getTotalPage())
	                {
	                    pageInfo.append(MessageFormat.format("<a   href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;text-decoration: none;\">.&nbsp;.</a> ", (model.getPageFirst() + model.getDispPageCount()), model.getTotalPage()));
	                }
	                if (model.getCpage() < model.getTotalPage())
	                {
	                    pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{1})\" style=\"margin-right:10px;\">后页</a> ", (model.getCpage() + 1), model.getTotalPage()));
	                    pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{0})\" style=\"margin-right:10px;\">尾页</a> ", model.getTotalPage()));
	                }
	                else
	                {
	                	pageInfo.append("<a disabled=\"disabled\" style=\"margin-right:10px;\">后页</a> ");
	                	pageInfo.append("<a disabled=\"disabled\" style=\"margin-right:10px;\">尾页</a> ");
	                }

	                pageInfo.append(MessageFormat.format("   <input   type=\"text\"  onclick=\"this.select()\" value=\"{0}\" name=\"CurrentPageIndex\" id=\"CurrentPageIndex\" style=\"width:30px;\" /><span>页</span>", model.getCpage()));
	                pageInfo.append(MessageFormat.format("   <input  id='page.DispPageCount' name=\"DispPageCount\" type=\"hidden\" value=\"{0}\" />", model.getDispPageCount()));
	                pageInfo.append(MessageFormat.format("   <input  id='page.PageSize' name=\"PageSize\" type=\"hidden\" value=\"{0}\" />", model.getPageSize()));
	                pageInfo.append(MessageFormat.format("   <input  type=\"submit\"  value=\"Go\" id=\"mvcpager_btnchk\" onclick=\"return checkInput({0},{1})\" /> ", model.getTotalPage(),  0));
	                pageInfo.append("</div>");
	            }
	          //  return new HtmlString(pageHtml.ToString());

			out.print(pageInfo.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
		
	}

	private int pageSize=10;//每页大小
	private int cpage;
	private int totalCount;

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCpage() {
		return cpage;
	}
	public void setCpage(int cpage) {
		this.cpage = cpage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
}
